package com.neuedu.his.controller;


import com.neuedu.his.entity.Menu;
import com.neuedu.his.entity.Role;
import com.neuedu.his.framework.PageInfo;
import com.neuedu.his.framework.annotation.MethodAlias;
import com.neuedu.his.framework.servlet.BaseServlet;
import com.neuedu.his.service.MenuService;
import com.neuedu.his.service.RoleService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * 项目：      his
 * 类名:       UserController
 * 创建时间：  2020/12/22  11:05
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@WebServlet(name="MenuController" ,urlPatterns = "/menu")
public class MenuController extends BaseServlet {

    private MenuService menuService = new MenuService();


    public void getUserMenus(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Menu> menuList = menuService.getUserMenus(request);
        //将结果以json格式返回
        this.success(response, menuList);
    }



    public void queryList(HttpServletRequest request, HttpServletResponse response) throws IOException {

       List<Menu> menuList = menuService.queryList(request);
        //将结果以json格式返回
        this.success(response, menuList);
    }

    @MethodAlias("query")
    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {

        PageInfo pageInfo = menuService.queryListByPage(request);
        //将结果以json格式返回
        this.success(response, pageInfo);
    }


    public void save(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {

        Menu menu = parseParameter(request, Menu.class);

        boolean success = false;
        if (menu.getMenuId() != null) { //修改
            success = menuService.update(menu);
        } else { //添加
            success = menuService.save(menu);
        }

        super.success(response, success);
    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {

        String menuId = request.getParameter("menuId");

        Menu menu  = menuService.queryById(Integer.parseInt(menuId));

        super.success(response, menu);
    }


    public void del(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {

        String menuId = request.getParameter("menuId");

       boolean success = menuService.deleteById(Integer.parseInt(menuId));

        super.success(response, success);
    }


}